﻿using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using BasicData.Domain.Entities;
using BasicData.Infrasturctrue;

namespace BasicData.Infrastructure.Interfasce
{
    /// <summary>
    /// 通用仓储接口
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    public interface IBaseRepositroy<T> where T : class
    {
        #region 查询操作

        MyDbContext Db { get; }


        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>实体对象</returns>
       IQueryable<T> GetByIdAsync(object id);

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns>实体集合</returns>
        IQueryable<T> GetAllAsync();

        /// <summary>
        /// 获取符合条件的第一个实体
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>实体对象</returns>
        IQueryable<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns>记录总数</returns>
        IQueryable<int> CountAsync();

        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>记录数</returns>
        IQueryable<int> CountAsync(Expression<Func<T, bool>> predicate);

        #endregion

        #region 添加操作

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>添加后的实体</returns>
        Task<int> AddAsync(T entity);

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>添加后的实体集合</returns>
        Task<int> AddRangeAsync(List<T> entities);

        #endregion

        #region 更新操作

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>更新后的实体</returns>
        Task<int> UpdateAsync(T entity);
        
        /// <summary>
        /// 更新实体（同步方法）
        /// </summary>
        /// <param name="entity">实体对象</param>
        void Update(T entity);

        /// <summary>
        /// 批量更新实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>更新后的实体集合</returns>
        Task<int> UpdateRangeAsync(List<T> entities);

        #endregion

        #region 删除操作

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>操作结果</returns>
        Task<int> DeleteAsync(T entity);

        /// <summary>
        /// 批量删除实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>操作结果</returns>
        Task<int> DeleteRangeAsync(IEnumerable<T> entities);

        /// <summary>
        /// 根据ID删除实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>操作结果</returns>
        Task<int> DeleteByIdAsync(object id);
        Task FindAsync(Func<object, bool> value);

        #endregion

        /// <summary>
        /// 保存所有更改
        /// </summary>
        /// <returns>受影响的行数</returns>
        Task<int> SaveAsync();
    }
}